<template>
  <h1>首页</h1>
  <div v-if="user==null">
    <router-link to="/login" style="text-decoration: none">登录</router-link>
    <router-link to="/reg" style="text-decoration: none">注册</router-link>
  </div>
  <div v-else>
    <router-link to="/post" style="text-decoration: none">
      <el-button>发布评论</el-button>
    </router-link>
    <el-button @click="logout">退出登录</el-button>
  </div>
  <div v-for="weibo in weibos">
    <p>{{weibo.username}}说: {{weibo.content}}</p>
  </div>
</template>

<script setup>
import {onMounted, ref} from "vue";
import axios from "axios";

const user = ref(localStorage.user ? JSON.parse(localStorage.user) : null);
console.log(user);
// 退出功能
const logout = () => {
  if (confirm("请确认要退出登录")) {
    localStorage.clear();
    user.value = null;
  }
}
const weibos = ref();
onMounted(()=>{
  axios.post("http://localhost:8080/v1/weibos/list")
      .then((response)=>{
        if(response.data.code==2001) {
            weibos.value = response.data.data;
        }
      })
})
</script>